## Figure 2: Types of 311 Demands by Neighborhood Race and Income

## install.packages(c("tidyverse"))
## library(tidyverse)

## SET WORKING DIRECTORY HERE
## setwd()

## Loading data
## load("dta.RData")

## Figure 2
demand_fig2 = dta %>%
  filter(top10 == 1) %>%
  filter(!is.na(city_service)) %>%
  mutate(non_white = case_when(white_third %in% c("1") ~ 1,
                               TRUE ~ 0),
         white = case_when(white_third %in% c("3") ~ 1,
                           TRUE ~ 0),
         poor = case_when(inc_third %in% c("1") ~ 1,
                          TRUE ~ 0),
         rich = case_when(inc_third %in% c("3") ~ 1,
                          TRUE ~ 0),
         n = 1) %>%
  group_by(service, city) %>%
  summarise(pct_poor = (sum(poor))/(sum(n)),
            pct_rich = (sum(rich))/(sum(n)),
            pct_white = (sum(white))/(sum(n)),
            pct_non_white = (sum(non_white))/(sum(n))) %>%
  ungroup() %>%
  mutate(poor_demand = (pct_poor - pct_rich)*100,
         non_white_demand = (pct_non_white - pct_white)*100) %>%
  select(city, service, poor_demand, non_white_demand)

## Figure 2a
race_demand = demand_fig2 %>%
  ggplot(aes(x = service, y = non_white_demand)) +
  facet_wrap(city ~ ., scales = "free", ncol = 2) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  labs(x = "Service",
       y = "Non-White Call Share - White Call Share") +
  theme_classic() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"))

## Figure 2b
poor_demand = demand_fig2 %>%
  ggplot(aes(x = service, y = poor_demand)) +
  facet_wrap(city ~ ., scales = "free", ncol = 2) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  labs(x = "Service",
       y = "Poor Call Share - Rich Call Share") +
  theme_classic() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"))
